<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div>
                <header style="height: 100%">
                    <div align="left">
	                    <table style="width: 100%">
	                   		<tr>
	                   			<td>
		                   			<form class="form-inline" id="searchForm" onsubmit="return false">
										<div class="form-group" style="width:95%;">
											流程业务类型：
											<select id="flowType">
												<option value="">全部</option>
											</select>
											流程名称：
											<input id="keys" type="search" class="form-control" placeholder="流程名称">
											归属机构：<input id="followOrgIds" type="text" class="form-control" style="width:30%;" followOrgIds="" onclick="openIframeOrgList(this)" readonly="readonly" placeholder='点击选择'>
											流程状态：
											<select id="status">
											</select>
											<button id="searchRest" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe9aa;</i>重置</button>
											<button id="searchBt" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe615;</i>搜索</button>
										</div>
									</form>
	                   			</td>
	                   			<td align="right">
	                   				<button class="layui-btn layui-btn-sm" onclick="location.href='addFlow.html'">
                                      <i class="layui-icon">&#xe608;</i> 添加流程
                                    </button>
	                   			</td> 
	                   		</tr> 
	                    </table>
					</div>
                </header>
                
                <div>
                    <div class="widget-body no-padding">
                        <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                            <thead>
                                <tr>
                                </tr>                       
                                <tr>
									<th>流程编码</th>
									<th>流程业务</th>
									<th>流程名称</th>
									<th>流程归属</th>
									<th>流程状态</th>
									<th>创建人</th>
									<th>创建时间</th>
									<th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript">
var pers = checkPermission();
var userStatus = showDictSelect("status", "userStatus", true);
var example;
function init(){
	example = 
    	$('#dt-table').DataTable({
        	"searching": false,
        	"processing": false,
        	"serverSide" : true,
        	"ordering" : false,
        	"language": {
                "url": urlRootPrefix + "/js/plugin/datatables/Chinese.lang"
            },
        	"ajax": {
        		"url" : urlRootPrefix + "/flows",
        		"type":"get",
        		"data":function(d){
    				d.selectTypeId = $("#flowType").val();
    				d.childOrgIds = $("#followOrgIds").attr("followOrgIds");
    				d.status = $("#status").val();
    				d.keywords = $("#keywords").val();
    			},
    			"dataSrc" : function(data) {
    				  //自定义错误拦截
     		          if(!data.success && !isNotBlank(data.recordsTotal)){
     		           	layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
     					return [];
     				  }
     		          return data.data;
      			} 
      			
        	},
        	"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",	
            "columns": [
				{"data" : "flowCode", "defaultContent" : ""},
				{"data" : "flowTypeName", "defaultContent" : ""},
				{"data" : "flowName", "defaultContent" : "",
		         	"render": function (data, type, row) {
		         		var href = "auditLogList.html?id="+row['flowId'];
                        return "<a href='javascript:void(0);' style='color:#3276b1;' onclick='showWindow(\""+href+"\",\"操作日志\")'>"+data+"</a>";
      	            }		
				},
				{"data" : "followOrgName", "defaultContent" : ""},
				{"data" : "statusName", "defaultContent": ""},
				{"data" : "createUserName", "defaultContent" : ""},
				{"data" : "createTime", "defaultContent" : ""},
				{"data" : "", 
					"defaultContent": "",
					"orderable":false,
			        "render": function (data, type, row) {
             		         var id = row['flowId'];//id
                             var edit = buttonEdit("updateFlow.html?id=" + id, "", pers);//修改
             		         var del = buttonDel(id, "", pers);//删除
             		         var showHref = "hccflow/page/design.html?flowId=" + id;
             		         var attr = buttonShow(showHref+"&status=0", "", pers,"&#xe614;","设计流程"); //设计流程 sys:flow:design
             		         var show = buttonShow(showHref+"&status=1", "", pers,"","查看流程"); //查看流程设计图
             		         var status = buttonSubStatus(id,row['status'], "", pers);//停用启用
                             return edit + del + attr + show + status;
               	    }		
				},
                
            ],
           "order": [[ 0, "asc" ]]
        } );
}

layui.use('layer', function(){
    var layer = layui.layer;
});

function del(id){
	layer.confirm('确定要删除吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
    	$.ajax({
            type : 'delete',
            url : urlRootPrefix + '/flows/'+id,
            success : function(data) {
            	if(!data.success){
					layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
					$("button[type='submit']").removeAttr("disabled");
					return;
				}
                example.ajax.reload();
                layer.msg("删除成功");
            }
        });
        
        layer.close(1);
    });
}
function getFlowTaskType(){
   	$.ajax({
       type : 'get',
       url : urlRootPrefix + '/flowTaskTypes/listVo',
       success : function(data) {
          if(!data.success){
           	layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
			return;
		  }
          var data = data.data;
          var typeHtml = "";
          for(var i=0;i<data.length;i++){
           typeHtml += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
          }
          $("#flowType").append(typeHtml);
        }
    });
}



$(function() {
	getFlowTaskType();
	init();
	$("#searchBt").click(function(){
		example.ajax.reload();
	});
	$("#searchRest").click(function(){
		$('#searchForm')[0].reset();
		$("#followOrgIds").attr("followOrgIds","");
	});
});
function openIframeOrgList(f) {
	var dt = {'ids':$(f).attr("followOrgIds"),'names':$(f).val()};
	layer.open({
		type : 2,
		title : "选择归属机构",
		shadeClose : false,
		shade : 0.8,
		maxmin : false, //开启最大化最小化按钮
		area : [ '500px', '650px' ],
		content : urlRootPrefix + "/pages/common/orgList.html?checkbox=1",
		success: function ( layero,index) {
        	//找到它的子窗口的body
            var body = layer.getChildFrame('body', index), iframeWin = window[layero.find('iframe')[0]['name']];  
          	//为子窗口元素赋值
            body.contents().find("#ids").val(dt.ids);
            body.contents().find("#names").val(dt.names);
            //调用子窗口的事件
            iframeWin.initData();
            iframeWin.init();
        },
		btn : [ '确定', '取消' ],
		yes : function(index, layero) {
			//按钮【按钮一】的回调
			var body = layer.getChildFrame('body', index);
			$("#followOrgIds").attr("followOrgIds",body.contents().find("#ids").val());
			$("#followOrgIds").val(body.contents().find("#names").val());
			layer.close(index);
		}
	});
}
function subStatus(id,oldStatus){
	var smsg;
	if(oldStatus == "C"){
		smsg = "是否确定停用？";
	}else{
		smsg = "是否确定启用？启用该流程会将该机构其他流程的状态默认改为停用";
	}
	layer.confirm(smsg, {
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({
		       type : 'delete',
		       url : urlRootPrefix + '/flows/status/'+id,
		       success : function(data) {
		          if(!data.success){
		           	layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
					return;
				  }
		          example.ajax.reload();
		          layer.msg("操作成功");
		        }
		 });
		layer.close(1);
	});
  }
</script>
